package com.xu.cloud.common.validation.validator;

import com.xu.cloud.common.validation.HasOptionalValue;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.Arrays;

/**
 * @author xuguan
 * @since 2023/11/10
 */
public class HasOptionalValueValidator implements ConstraintValidator<HasOptionalValue, String> {

    private String[] optionalValues = {};

    @Override
    public void initialize(HasOptionalValue hasOptionalValue) {
        optionalValues = hasOptionalValue.optionalValues();
    }

    @Override
    public boolean isValid(String value, ConstraintValidatorContext context) {
        return Arrays.asList(optionalValues).contains(value);
    }
}
